package com.ncut.data.service.impl;

import com.ncut.api.client.ProductClient;
import com.ncut.api.client.TradeClient;
import com.ncut.api.client.UserClient;
import com.ncut.constant.StatusConstant;
import com.ncut.entity.Orders;
import com.ncut.vo.BusinessDataVO;
import com.ncut.vo.DishOverViewVO;
import com.ncut.vo.OrderOverViewVO;
import com.ncut.vo.SetmealOverViewVO;
import com.ncut.data.service.WorkSpaceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @FileName WorkSpaceServiceImpl
 * @Description
 * @Author Sword
 * @date 2025-07-28
 **/

@Service
@Slf4j
public class WorkSpaceServiceImpl implements WorkSpaceService {

/*    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;*/

    @Autowired
    private TradeClient tradeClient;

    @Autowired
    private UserClient userClient;

    @Autowired
    private ProductClient productClient;


    /**
     * 查询今日运营数据
     *
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        Map map = new HashMap();
        map.put("end", end);

        //查询总订单数
        Integer totalOrderCount = tradeClient.countByMap(map);

        map.put("begin", begin);

        //查询新增用户数
        Integer newUsers = userClient.countByMap(map);

        //查询有效订单
        map.put("status", Orders.COMPLETED);
        Integer validOrderCount = tradeClient.countByMap(map);

        //订单完成率
        Double orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount == 0.0 ? 0.0 : validOrderCount.doubleValue() / totalOrderCount;

        //营业额
        Double turnover = tradeClient.sumByMap(map);
        turnover = turnover == null ? 0.0 : turnover;


        //平均客单价
        Double unitPrice = turnover.doubleValue() / validOrderCount;

        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .turnover(turnover)
                .build();

    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    @Override
    public SetmealOverViewVO getSetmealOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = productClient.setmealCountByMap(map);
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = productClient.setmealCountByMap(map);
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    @Override
    public DishOverViewVO getDishOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = productClient.dishCountByMap(map);
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = productClient.dishCountByMap(map);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询订单管理数据
     *
     * @return
     */
    @Override
    public OrderOverViewVO getOrderOverView() {
        Map map = new HashMap();

        //查询订单总数
        map.put("status", null);
        Integer allOrders = tradeClient.countByMap(map);

        //查询待接单数量
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = tradeClient.countByMap(map);

        //查询待派送数量
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = tradeClient.countByMap(map);

        //查询已完成数量
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = tradeClient.countByMap(map);

        //查询已取消数量
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = tradeClient.countByMap(map);

        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .build();
    }
}
